﻿using Jw.Core;
using Jw.Models;
using Jw.Web.Areas.Dev.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

using Newtonsoft.Json.Linq;
using Jw.Web.App_Start;

namespace Jw.Web.Areas.Dev.Controllers
{
    public class YxjlController : Controller
    {
        //
        // GET: /Dev/Yxjl/

        public ActionResult Index()
        {
            var codeService = new sys_codeService();
            var model = new
            {
                idField = "Yxjl_BillNo", //主键
                //urls = DevHelper.GetIndexUrls("Yxjl"),
                resx = DevHelper.GetIndexResx("运行记录"),
                dataSource = new
                {
                    yearItems = codeService.GetYearItems(),
                    monthItems = codeService.GetMonthItems(),
                    sbztItems = codeService.GetTextListByType("Sbzt")
                },
                urls = new
                {
                    query = "/api/Dev/Yxjl",
                    billno = "/api/Dev/Yxjl/getnewbillno",
                    audit = "/api/Dev/Yxjl/audit/",
                    edit = "/api/Dev/Yxjl/edit/"
                },
                form = new
                {
                    Yxjl_BillNo = "",
                    Yxjl_Sbbh = ""
                },
                defaultRow = new
                {
                    Yxjl_Jlrq = DateTime.Now.Date.ToString("yyyy-MM-dd"),
                    Yxjl_Zdts = 0.00,
                    Yxjl_Yxts = 0.00,
                    Yxjl_Tjts = 0.00,
                    Yxjl_Jhcl = 0.00,
                    Yxjl_Sjcl = 0.00,
                    Yxjl_Scnl = 0.00
                },
                setting = new
                {
                    postListFields = new string[] { "Yxjl_BillNo", "Yxjl_Sbbh", "Yxjl_Jlrq", "Yxjl_Zdts", "Yxjl_Yxts",
                    "Yxjl_Tjqk","Yxjl_Tjyy","Yxjl_Tjts","Yxjl_Jhcl","Yxjl_Sjcl","Yxjl_Scnl","Yxjl_Sbzt","Yxjl_Remark"}
                }

            };
            return View(model);
        }


    }

    public class YxjlApiController : ApiController
    {
        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Yxjl_BillNo'>
                <select>
                   A.*,B.Sbda_Sbmc,B.Sbda_Ggxh
                </select>
                <from>
                   b_Yxjl A
                   left join b_Sbda B on B.Sbda_Sbbm = A.Yxjl_Sbbh
                </from>
                <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                    <field name='Yxjl_BillNo'   cp='like'  ></field>
                    <field name='CreatePerson'  cp='like'  ></field>
                </where>
            </settings>
            ");
            var pQuery = query.ToParamQuery();
            var list = new b_YxjlService().GetDynamicListWithPaging(pQuery);
            return list;

        }
        public string GetNewBillNo()
        {
            return new b_YxjlService().GetNewKey("Yxjl_BillNo", "dateplus");
        }
        public void Edit(dynamic data)
        {
            var listWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Yxjl_BillNo' cp='equal'></field></where>
            </settings>", typeof(b_Yxjl).Name);
            var service = new b_YxjlService();
            var result = service.Edit(null, listWrapper, data);

        }

        public void Delete(string id)
        {
            var result = new b_YxjlService().Delete(ParamDelete.Instance().AndWhere("Yxjl_BillNo", id));
            DevHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Yxjl_BillNo={0}]，请重试或联系管理员！", id);
        }

        //审核
        [System.Web.Http.HttpPost]
        public void Audit(string id, JObject data)
        {
            var status = data["status"].ToString();
            var comment = data["comment"].ToString();
            var result = new DevService().Audit(typeof(b_Yxjl).Name, "Yxjl_BillNo", id, status, comment);
            DevHelper.ThrowHttpExceptionWhen(result < 0, "单据审核失败[Yxjl_BillNo={0}]，请重试或联系管理员！", id);
        }

    }
}
